using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using eee.Sheffield.PZ.Imaging.ImageFilter;

namespace eee.Sheffield.PZ.QAAS
{
    public partial class ThresholdFilterControl : FilterControl
    {
        #region Fields
        private int _threshold;
        #endregion

        #region Properties
        public int ThresholdValue { get { return _threshold; } }
        #endregion

        #region Constructor
        public ThresholdFilterControl() 
        { 
            InitializeComponent(); 
            _flag = "Threhold Filter";
        }

        public ThresholdFilterControl(MainForm mainForm, ImageDoc imageDoc)
            : base(mainForm, imageDoc)
        {
            InitializeComponent();
            _flag = "Threhold Filter";
        } 
        #endregion

        #region interface methods
        public override void Reload() { }
        #endregion

        #region button methods
        // do threshold
        private void buttonOK_Click(object sender, EventArgs e)
        {
            _threshold = Convert.ToInt16(txtThreshold.Text);
            
            Bitmap srcImage = _imageDoc.Image;
            // apply threshold filter
            eee.Sheffield.PZ.Imaging.ImageFilter.Threshold thresholdFilter = new Threshold(_threshold);
            Bitmap copySrcImage = (Bitmap)srcImage.Clone();
            eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref copySrcImage);
            Bitmap dstImage = thresholdFilter.ApplyGraylevelImageFilter(copySrcImage);
            if (dstImage.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
                eee.Sheffield.PZ.Imaging.Image.GenerateGraylevelPalette(ref dstImage);
            _log = thresholdFilter.LogText;
           
            // display image
            string thresholdTxt = "Threshold at " + _threshold;
            _mainForm.ShowImage(dstImage, thresholdTxt);
            _mainForm.Log(_log);
        } // buttonOK_Click()

        // do threshold by SIS
        private void buttonSIS_Click(object sender, EventArgs e)
        {
            Bitmap srcImage = _imageDoc.Image;
            // apply filter
            SISThreshold thresholdFilter = new SISThreshold();
            Bitmap copySrcImage = (Bitmap)srcImage.Clone();
            eee.Sheffield.PZ.Imaging.Image.StandardizeImageTo8bbpIndexed(ref copySrcImage);
            Bitmap dstImage = thresholdFilter.ApplyGraylevelImageFilter(copySrcImage);
            if (dstImage.PixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
                eee.Sheffield.PZ.Imaging.Image.GenerateGraylevelPalette(ref dstImage);
            _log = thresholdFilter.LogText;
            _threshold = thresholdFilter.Threshold;
            txtThreshold.Text = ((int)_threshold).ToString();

            // display image
            string thresholdTxt = "Threshold at " + _threshold;
            _mainForm.ShowImage(dstImage, thresholdTxt);
            _mainForm.Log(_log);
        } // buttonSIS_Click()
        #endregion
    }
}
